#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext import db
import os
from datetime import date

# model #
class Category(db.Model):
    name = db.StringProperty()
    
class Link(db.Model):
    name = db.StringProperty()
    url = db.StringProperty()

class Blog(db.Model):
    title = db.StringProperty()
    body = db.TextProperty()
    created_at = db.DateProperty()
    category = db.ReferenceProperty(Category)
    
# handler
class MainHandler(webapp.RequestHandler):
    def get(self):
        categories = db.GqlQuery("SELECT * FROM Category")
        links = db.GqlQuery("SELECT * FROM Link")
        blogs = db.GqlQuery("SELECT * FROM Blog")
        template_values={"categories":categories, "links":links, "blogs":blogs}
        path = os.path.join(os.path.dirname(__file__), 'views/index.html')
        self.response.out.write(template.render(path, template_values))

# list all categories        
class CategoryHandler(webapp.RequestHandler):
    def get(self):
        categories = db.GqlQuery("SELECT * FROM Category")
        template_values={"categories":categories}
        path = os.path.join(os.path.dirname(__file__), 'views/admin/categoryview.html')
        self.response.out.write(template.render(path, template_values))
        
# add category
class CategoryAddHandler(webapp.RequestHandler):
    def post(self):
        category = Category()
        name = self.request.get('category')
        if name:
            category.name = name
            category.put()
        self.redirect('/admin')

# delete category
class CategoryDeleteHandler(webapp.RequestHandler):
    def get(self, key):
        if key:
            db.delete(key)
        self.redirect('/admin')
        
# list all links
class LinkHandler(webapp.RequestHandler):
    def get(self):
        links = db.GqlQuery("SELECT * FROM Link")
        template_values = {"links":links}
        path = os.path.join(os.path.dirname(__file__), 'views/admin/linkview.html')
        self.response.out.write(template.render(path, template_values))
        
# add link
class LinkAddHandler(webapp.RequestHandler):
    def post(self):
        link = Link()
        name = self.request.get('title')
        url = self.request.get('link')
        if name and link:
            link.name = name
            link.url = url
            link.put()
        self.redirect('/listlink')

# delete link
class LinkDeleteHandler(webapp.RequestHandler):
    def get(self, key):
        if key:
            db.delete(key)
        self.redirect('/listlink')
        
# list all blogs
class BlogHandler(webapp.RequestHandler):
    def get(self):
        blogs = db.GqlQuery("SELECT * FROM Blog")
        categories = db.GqlQuery("SELECT * FROM Category")
        template_values = {"blogs":blogs, "categories":categories}
        path = os.path.join(os.path.dirname(__file__), 'views/admin/blogview.html')
        self.response.out.write(template.render(path, template_values))
        
# add blog
class BlogAddHandler(webapp.RequestHandler):
    def post(self):
        blog = Blog()
        title = self.request.get("title")
        body = self.request.get("body")
        category = Category.get(self.request.get("category"))
        if title and body:
            blog.title = title
            blog.body = body
            blog.created_at = date.today()
            blog.category = category
            blog.put()
        self.redirect('/listpost')
        
# delete blog
class BlogDeleteHandler(webapp.RequestHandler):
    def get(self, key):
        if key:
            db.delete(key)
        self.redirect('/listpost')
      
def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                          ('/admin', CategoryHandler),
                                          ('/addcategory', CategoryAddHandler),
                                          (r'/deletecategory/(.*)', CategoryDeleteHandler),
                                          ('/listlink', LinkHandler),
                                          ('/addlink', LinkAddHandler),
                                          (r'/deletelink/(.*)', LinkDeleteHandler),
                                          ('/listpost', BlogHandler),
                                          ('/addpost', BlogAddHandler),
                                          (r'/deletepost/(.*)', BlogDeleteHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
